//
//  ByReleaseGoodsNewVC.m
//  mijifen_ios
//
//  Created by mc on 2017/11/6.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByReleaseGoodsNewVC.h"

// pod
#import "UITableView+FDTemplateLayoutCell.h"

// view
#import "ByViewGoodsName.h"             // 商品名称
#import "ByViewGoodsInfo.h"             // 价格／原价／库存／库存预警
#import "ByViewGoodsCondition.h"        // 当日达
#import "ByViewGoodsClassify.h"         // 商品分类

// cell
#import "ByCellGoodsDefine.h"           // 三级分类／商品参数／参数值

// vc
#import "ByGoodsListVC.h"               // 商品列表


@interface ByReleaseGoodsNewVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray   *titleArray;

@property (nonatomic, strong) NSMutableArray *thirdArray;       // 三级分类数组
@property (nonatomic, strong) NSMutableArray *productArray;     // 商品参数数组
@property (nonatomic, strong) NSMutableArray *paramArray;       // 参数数组
@end

@implementation ByReleaseGoodsNewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发布商品";
    [self addLeftBackBarButtonItem];
    [self addBYItemviews:@[@"列表"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    [self initTable];
}

#pragma mark - getter
- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"三级分类",@"商品参数",@"参数值"];
    }
    return _titleArray;
}

- (NSMutableArray *)thirdArray{
    if (!_thirdArray) {
        _thirdArray = [NSMutableArray array];
        _thirdArray = [@[@"笔记本",@"台式机",@"超级本",@"平板",@"哈哈",@"测试测试测试呀",@"iMac pro",@"MacBook",@"iPad mini",@"iPad",@"iphonex",@"笔记本",@"台式机",@"超级本",@"平板",@"哈哈",@"测试测试测试呀",@"iMac pro",@"MacBook",@"iPad mini",@"iPad",@"iphonex"] mutableCopy];
    }
    return _thirdArray;
}

- (NSMutableArray *)productArray{
    if (!_productArray) {
        _productArray = [NSMutableArray array];
        _productArray = [@[@"13寸",@"14寸",@"15寸",@"13寸",@"14寸",@"15寸"] mutableCopy];
    }
    return _productArray;
}

- (NSMutableArray *)paramArray{
    if (!_paramArray) {
        _paramArray = [NSMutableArray array];
        _paramArray = [@[@"白色",@"红色",@"蓝色"] mutableCopy];
    }
    return _paramArray;
}
#pragma mark -
- (void)rightItemsAction:(NSInteger)rightIndex{
    ByGoodsListVC *vc = [[ByGoodsListVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}

#pragma mark - 创建视图
- (void)initTable{
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-50);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom);
        }
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[ByCellGoodsDefine class] forCellReuseIdentifier:@"ByCellGoodsDefine"];
    
    ByViewGoodsName *nameView = [[ByViewGoodsName alloc] init];
    nameView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 195 + 50 + 20 + 5 + 10);
    self.tableView.tableHeaderView = nameView;
    
    UIButton *commitButton = [[UIButton alloc] init];
    [self.view addSubview:commitButton];
    [commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
        if (@available(iOS 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.bottom.equalTo(self.view.mas_bottom);
        }
    }];
    [commitButton setTitle:@"提交" forState:UIControlStateNormal];
    [commitButton setTitleColor:BY_TEXT_COLOR forState:UIControlStateNormal];
    [commitButton setBackgroundColor:BY_THEME_COLOR];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 7;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentier = @"customCell";
    UITableViewCell *customCell = [tableView dequeueReusableCellWithIdentifier:indentier];
    if (customCell == nil) {
        customCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentier];
        customCell.selectionStyle = UITableViewCellSelectionStyleNone;
        customCell.backgroundColor = [UIColor clearColor];
    }
    for (UIView *subview in customCell.subviews) {
        [subview removeFromSuperview];
    }
    // 价格
    if (indexPath.section == 0) {
        ByViewGoodsInfo *infoView = [[ByViewGoodsInfo alloc] init];
        infoView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
        [customCell addSubview:infoView];
        infoView.editBlock = ^(NSMutableDictionary *infoDic) {
            NSLog(@"%@",infoDic);
        };
    }
    // 当日达
    else if (indexPath.section == 1){
        ByViewGoodsCondition *conditionView = [[ByViewGoodsCondition alloc] init];
        conditionView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 260);
        [customCell addSubview:conditionView];
        conditionView.photoBlock = ^(UIImage *img) {
            NSLog(@"%@",img);
        };
        
        conditionView.sendBlock = ^(NSInteger type) {
            NSLog(@"%ld",(long)type);
        };
        
        conditionView.editEndBlock = ^(NSMutableDictionary *dic) {
            NSLog(@"%@",dic);
        };
        
        conditionView.recommendBlock = ^(BOOL isRecommend) {
            NSLog(@"%@",isRecommend == YES?@"yes":@"false");
        };
    }
    // 商品分类
    else if(indexPath.section == 2){
        ByViewGoodsClassify *classifyView = [[ByViewGoodsClassify alloc] init];
        classifyView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 150);
        [customCell addSubview:classifyView];
        // 测试
        ByModelGoodsClassify *model = [[ByModelGoodsClassify alloc] init];
        classifyView.model = model;
    }
    // 商品详情
    else if(indexPath.section == 6){
    }
    // 三级分类／商品参数／参数值
    else{
        ByCellGoodsDefine *cell = [ByCellGoodsDefine cellWithTableView:tableView];
        cell.title = self.titleArray[indexPath.section - 3];
        __weak typeof(self) weakSelf = self;
        // 三级分类
        if (indexPath.section == 3) {
            cell.items = self.thirdArray;
            cell.editTap = ^(NSString *str) {
                [weakSelf.thirdArray addObject:str];
                NSIndexSet *inset = [NSIndexSet indexSetWithIndex:indexPath.section];
                [weakSelf.tableView reloadSections:inset withRowAnimation:UITableViewRowAnimationNone];
            };
        }
        // 商品参数
        else if (indexPath.section == 4){
            cell.items = self.productArray;
            cell.editTap = ^(NSString *str) {
                [weakSelf.productArray addObject:str];
                NSIndexSet *inset = [NSIndexSet indexSetWithIndex:indexPath.section];
                [weakSelf.tableView reloadSections:inset withRowAnimation:UITableViewRowAnimationNone];
            };
        }
        // 参数值
        else{
            cell.items = self.paramArray;
            cell.editTap = ^(NSString *str) {
                [weakSelf.paramArray addObject:str];
                NSIndexSet *inset = [NSIndexSet indexSetWithIndex:indexPath.section];
                [weakSelf.tableView reloadSections:inset withRowAnimation:UITableViewRowAnimationNone];
            };
        }
        return cell;
    }
        
        
   
    return customCell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 100;
    }else if (indexPath.section == 1){
        return 260;
    }else if (indexPath.section == 2){
        return 150;
    }else if (indexPath.section == 6){
        return 195;
    }else{
        if (indexPath.section == 3) {
            return  [ByCellGoodsDefine getHeightFromData:self.thirdArray] + 40;

        }else if (indexPath.section == 4){
            return  [ByCellGoodsDefine getHeightFromData:self.productArray] + 40;

        }
        return  [ByCellGoodsDefine getHeightFromData:self.paramArray] + 40;

    }
 
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}



@end
